package jp.sourceforge.akjrcp.snack;

import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import jp.sourceforge.akjrcp.ffmpeg.FFMpegUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;



/*
 * 
 *0.025 sec is skipped.
 */
public class PitchExecuter {
	public static Log log=LogFactory.getLog(PitchExecuter.class);
	private String pitch2ExePath;
	public static double SKIPPED_TIME=0.025;
	private Process process;
	public String[] getCommands(String wavePath,int start,int end,int min,int max,double framelength){
		String argv[]={pitch2ExePath,wavePath,""+start,""+end,""+min,""+max,""+framelength};
		return argv;
	}
	public void cancel(){
		if(process!=null){
			process.destroy();
		}
	}
	public ESPS[] getESPS(String wavePath,int start,int end,int min,int max,double framelength) throws IOException{
		ESPSParser parser=new ESPSParser();
		String argv[]=getCommands(wavePath,start,end,min,max,framelength);
		
		process = Runtime.getRuntime().exec(argv);
			InputStream input=null;
	        synchronized(this){
	            if(process!=null){
	            input=process.getInputStream();
	            if(input!=null){
	                long starttime=System.currentTimeMillis();
	                log.trace("start-parse-esps:");
	                Vector vector= (Vector) parser.parse(input);
	                log.trace("end-parse-esps:"+(System.currentTimeMillis()-starttime)+"ms");
	                return (ESPS[]) vector.toArray(new ESPS[vector.size()]);
	            }else{
	            	throw new IOException("input null");
	            }
	            }
	        }
		
		return null;
		}
	
	
	public static ESPS[] toAverage(ESPS[] espses,int averageSize){
		if(espses.length==0){
			return espses;
		}
	int arraySize=espses.length/averageSize;
	if(arraySize==0){
		return new ESPS[0];
		}
	ESPS[] retData=new ESPS[arraySize];
	for (int i = 0; i < retData.length; i++) {
		retData[i]=new ESPS();
		int voicing=0;
		int zero=0;
		for(int j=0;j<averageSize;j++){
			
			ESPS baseData=espses[i*averageSize+j];
			retData[i].setCorrelation(retData[i].getCorrelation()+baseData.getCorrelation());
			retData[i].setMeasurements(retData[i].getMeasurements()+baseData.getMeasurements());
			if(retData[i].getPitch()>0){
				retData[i].setPitch(retData[i].getPitch()+baseData.getPitch());
			}else{
				zero++;
			}
			if(baseData.isVoicing()){
				voicing++;
			}
			
		}
		if(zero<averageSize/2){
			retData[i].setPitch(retData[i].getPitch()/(averageSize-zero));
		}
		retData[i].setCorrelation(retData[i].getCorrelation()/averageSize);
		retData[i].setMeasurements(retData[i].getMeasurements()/averageSize);
		
		if(voicing>=averageSize/2){
			retData[i].setVoicing(true);
		}
	}
	return retData;
	}


	public String getPitch2ExePath() {
		return pitch2ExePath;
	}


	public void setPitch2ExePath(String pitch2ExePath) {
		this.pitch2ExePath = pitch2ExePath;
	}

		
}
/**
 * #pitch2.exe
#! /usr/bin/env python

from Tkinter import *
from tkSnack import *
root = Tkinter.Tk()

initializeSnack(root);
import sys;

length=len(sys.argv);

if length>1:
	path=sys.argv[1];
else:
	print "pitch2.py input [start end min max framelength]";
	sys.exit();

if length>2:
	startsample=int(sys.argv[2]);
else:
	startsample=0;

if length>3:
	endsample=int(sys.argv[3]);
else:
	endsample=0;

if length>4:
	min=int(sys.argv[4]);
else:
	min=60;

if length>5:
	max=int(sys.argv[5]);
else:
	max=600;

if length>6:
	flength=float(sys.argv[6]);
else:
	flength=0.01;

#guess min is 0.01
if(flength>0.01):
	flength=0.01;


mysound=Sound(load=path);
mysound.convert(encoding='Lin16',channels='1');

if endsample == 0:
	endsample=mysound.length();
elif endsample <0:
	endsample=mysound.length()+endsample;

#print endsample;
#framelength=0.01, 0.1 has error 
#windowlength=0.025,0 has error
resultList=mysound.pitch(method='ESPS',framelength=flength,minpitch=min,maxpitch=max,start=startsample,end=endsample);


for i in resultList:
	print i;


 */


